package com.zz.bsea.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zz.bsea.entity.User;
import com.zz.bsea.service.UserService;

public class LoginServlet extends HttpServlet {
	
	 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
	 {
		 String uname=req.getParameter("name");
		 String upwd=req.getParameter("pwd");
		 UserService us=new UserService();
		 User u=us.login(uname, upwd);
		 boolean isSuccess=false;
		 if(u!=null) {
			 isSuccess=true;
		 }
		 resp.setCharacterEncoding("utf-8");
		 resp.setContentType("text/html;charset=utf-8");
		 if(isSuccess) {
			 HttpSession ss=req.getSession();
			 ss.setAttribute("loginname", uname);
			 String url="/java11/index3.jsp";
			 if("老师".equals(u.getRole())) {
				 url="/java11/index.jsp";
			 }else if("学生".equals(u.getRole())) {
				 url="/java11/index3.jsp";
			 }
			//重定向
			 resp.sendRedirect(url);
		 }else {
			 resp.sendRedirect("/java11/BseaLogin.jsp");
		 }
		 
//		 resp.getWriter().append("登录结果："+isSuccess);
	 }
}
